package com.hospital.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hospital.entity.Appointment;
import com.hospital.service.AppointmentService;
import com.hospital.service.DoctorService;
import com.hospital.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class AppointmentController {
    @Autowired
    AppointmentService appointmentService;
    @Autowired
    DoctorService doctorService;
    @Autowired
    PatientService patientService;
    @RequestMapping("/admin/appointmentManage")
    public String appointmentManage(Model model,
                                    @RequestParam(value = "doctorname",required = false)String doctorname,
                                    @RequestParam(value = "patientname",required = false)String patientname,
                                    @RequestParam(value = "curr",required = false)String curr,//查询的页面
                                    @RequestParam(value = "size",required = false)String size //每页显示的记录数
                                    ){
        String currentPage = "1";//默认起始页
        String pageSize = "5";//默认每页显示记录数
        if (curr != null) {
            currentPage = curr;
        }
        if (size != null) {
            pageSize = size;
        }

        //第一个参数为页数，第二个为显示数
        PageHelper.startPage( Integer.parseInt(currentPage),
                Integer.parseInt(pageSize));
        //去数据库中查询数据（查询所有的记录）
        List<Appointment> appointmentList = appointmentService.getAllAppointments(doctorname,patientname);
        //获取分页后的数据和信息
        PageInfo pageInfo=new PageInfo(appointmentList);
//        System.out.println(pageInfo);
        //包装到request对象中
        model.addAttribute("appointments" ,pageInfo.getList());//数据集合
//        System.out.println(pageInfo.getList());
        model.addAttribute("count" ,pageInfo.getTotal());//记录总数
//        System.out.println(pageInfo.getTotal());
        model.addAttribute("currentPage" ,currentPage); //当前页
        model.addAttribute("doctorname",doctorname);
        model.addAttribute("patientname",patientname);

        return"admin/appointmentManage";
    }
    @RequestMapping("/admin/appointmentAdd")
    public String appointmentAddPage(HttpServletRequest request){
        request.setAttribute("patients",patientService.getAllPatients());
        //request.setAttribute("doctors",doctorService.getAllDoctor());
        return"admin/add/appointmentadd";
    }
    @RequestMapping(value = "/admin/appointment/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public JSONObject delAppointment(@PathVariable Integer id){
        JSONObject json=new JSONObject();
        json.put("message",appointmentService.delAppointment(id));
        return json;
    }
    @RequestMapping(value = "/admin/appointment/{id}",method = RequestMethod.GET)
    public String AppointmentInfo(@PathVariable Integer id,HttpServletRequest request){
        request.setAttribute("patients",patientService.getAllPatients());
        request.setAttribute("doctors",doctorService.getAllDoctor());
        request.setAttribute("appointment",appointmentService.getAppointment(id));
        return "admin/info/appointmentInfo";
    }
    @RequestMapping(value = "/admin/appointment",method = RequestMethod.PUT)
    @ResponseBody
    public JSONObject AppointmentUpdate(@RequestBody Appointment appointment){
        JSONObject json=new JSONObject();
        json.put("message",appointmentService.UpdateAppointment(appointment));
        return json;
    }
    @RequestMapping(value = "/admin/appointment",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject AppointmentAdd(@RequestBody Appointment appointment){
        JSONObject json=new JSONObject();
        json.put("message",appointmentService.addAppointment(appointment));
        return json;
    }
}
